import nyNetwork from "@/api/nyBaseNetwork";
import DZModelAdmin from "../model/DZModelAdmin";
import DZModelRole from "../model/DZModelRole";

class DZServiceAdmin {

    list_role: Array<DZModelRole> = []

    m_role = new DZModelRole();

    list_admin: Array<DZModelAdmin> = [];

    m_admin = new DZModelAdmin();


    net_list_role() {
        let arr_data = [];
        nyNetwork.net("角色列表", {}, data => {
            for (const item of data) {
                let data = new DZModelRole();
                data.iId = item.iid ?? null;
                data.sName = item.sname ?? "";
                data.iStatus = item.istatus ?? "";
                if (item.sinfo) {
                    data.sInfo = JSON.parse(item.sinfo);
                }
                arr_data.push(data);
            }
            this.list_role = arr_data;
        })
    }

    net_add_role(success: () => void) {
        nyNetwork.net("添加角色", this.m_role, data => {
            success();
        })
    }

    net_edit_role(success: () => void) {
        nyNetwork.net("修改角色", this.m_role, data => {
            success();
        })
    }

    net_operate_role(id, success: () => void) {
        nyNetwork.net("冻结/解冻角色", { id }, data => {
            success();
        })
    }

    net_del_role(id, success: () => void) {
        nyNetwork.net("删除角色", { id }, data => {
            success();
        })
    }

    net_list_admin() {
        let arr_data = [];
        nyNetwork.net("用户列表", {}, data => {
            for (const item of data) {
                let data = new DZModelAdmin();
                data.iId = item.iid ?? null;
                data.sAccount = item.saccount ?? "";
                data.roleName = item.roleName ?? "";
                data.iRoleId = item.iroleId ?? null;
                data.iStatus = item.istatus ?? 0;
                arr_data.push(data);
            }
            this.list_admin = arr_data;
        })
    }

    net_add_admin(success: () => void) {
        nyNetwork.net("添加用户", { ...this.m_admin }, data => {
            success();
        })
    }

    net_edit_admin(success: () => void) {
        nyNetwork.net("修改用户", { ...this.m_admin }, data => {
            success();
        })
    }

    net_operate_admin(id, success: () => void) {
        nyNetwork.net("冻结/解冻用户", { id }, data => {
            success();
        })
    }

    net_del_admin(id, success: () => void) {
        nyNetwork.net("删除用户", { id }, data => {
            success();
        })
    }

}
export default DZServiceAdmin;